package com.module.app.pop;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.StringRes;

import com.module.app.bean.IType;
import com.module.app.bean.IType;
import com.module.app.widget.dialog.AlertBuilder;
import com.module.base.cache.CacheSDK;
import com.module.app.widget.dialog.AlertBuilder;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;
import com.module.app.R;

public class CommonPop extends BottomPopupView {

    public static class Builder {
        protected AlertBuilder mController;

        public Builder(Context context) {
            mController = new AlertBuilder();
            mController.context = context;

        }

        public Builder setTitle(@StringRes int titleId) {
            mController.title = mController.context.getResources().getString(titleId);
            return this;
        }

        public Builder setTitle(CharSequence title) {
            mController.title = title;
            return this;
        }

        public Builder setContent(@StringRes int titleId) {
            mController.content = mController.context.getResources().getString(titleId);
            return this;
        }

        public Builder setContent(CharSequence title) {
            mController.content = title;
            return this;
        }

        public Builder setCancelButton(@StringRes int titleId) {
            mController.cancelText = mController.context.getResources().getString(titleId);
            return this;
        }

        public Builder setCancelButton(CharSequence title) {
            mController.cancelText = title;
            return this;
        }

        public Builder setConfirmButton(@StringRes int titleId) {
            mController.confirmText = mController.context.getResources().getString(titleId);
            return this;
        }

        public Builder setConfirmButton(CharSequence title) {
            mController.confirmText = title;
            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            mController.mCancelable = cancelable;
            return this;
        }

        public Builder setAutoDismiss(boolean isAutoDismiss) {
            mController.isAutoDismiss = isAutoDismiss;
            return this;
        }

        public Builder setCanceledOnTouchOutside(boolean mCanceledOnTouchOutside) {
            mController.mCanceledOnTouchOutside = mCanceledOnTouchOutside;
            return this;
        }

        public Builder setOnCancelListener(OnClickListener onCancelListener) {
            mController.mOnCancelListener = onCancelListener;
            return this;
        }

        public Builder setOnConfirmListener(OnClickListener mOnConfirmListener) {
            mController.mOnConfirmListener = mOnConfirmListener;
            return this;
        }

        /**
         * @see android.view.Gravity
         */
        public Builder setContentGravity(int gravity) {
            mController.gravity = gravity;
            return this;
        }

        public Builder setCacheStr(String cacheStr) {
            mController.cacheStr = cacheStr;
            return this;
        }

        public CommonPop create() {
            CommonPop pop = (CommonPop) new XPopup.Builder(mController.context)
                .hasShadowBg(true)
                .isDestroyOnDismiss(true)
                .dismissOnBackPressed(mController.mCancelable)
                .dismissOnTouchOutside(mController.mCanceledOnTouchOutside)
                .asCustom(new CommonPop(mController));
            return pop;
        }

        public void show() {
            create().show();
        }

        public void show(long delayMillis) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (mController.context == null || (mController.context instanceof Activity && ((Activity) mController.context).isFinishing())) {
                        return;
                    }
                    create().show();
                }
            }, delayMillis);
        }
    }


    private TextView tv_title;
    private TextView tv_content;
    private TextView tv_submit;
    private TextView tv_cancel;
    protected AlertBuilder builder;
    private OnDismissListener dismissListener;

    public interface OnDismissListener {
        void onDismiss();
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.app_pop_common;
    }

    public CommonPop(AlertBuilder builder) {
        super(builder.context);
        this.builder = builder;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        tv_title = findViewById(R.id.tv_title);
        tv_content = findViewById(R.id.tv_content);
        tv_submit = findViewById(R.id.tv_submit);
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_title = findViewById(R.id.tv_title);

        if (builder != null && !TextUtils.isEmpty(builder.title)) {
            tv_title.setText(builder.title);
        }
        if (builder != null && !TextUtils.isEmpty(builder.content)) {
            tv_content.setText(builder.content);
        }
        if (builder != null && !TextUtils.isEmpty(builder.cancelText)) {
            tv_cancel.setText(builder.cancelText);
            tv_cancel.setVisibility(View.VISIBLE);
        }
        if (builder != null && !TextUtils.isEmpty(builder.confirmText)) {
            tv_submit.setText(builder.confirmText);
        }
        tv_submit.setOnClickListener(view -> {
            onSubmitClick(view);
        });
        tv_cancel.setOnClickListener(view -> {
            if (builder != null && builder.mOnCancelListener != null) {
                builder.mOnCancelListener.onClick(view);
            }
            if (builder != null && builder.isAutoDismiss) {
                dismiss();
            }
        });

        tv_content.setGravity(builder.gravity);
    }

    private void onSubmitClick(View view) {
        if (builder != null && builder.mOnConfirmListener != null) {
            builder.mOnConfirmListener.onClick(view);
        }
        if (builder != null && builder.isAutoDismiss) {
            dismiss();
        }
        if (builder != null && !TextUtils.isEmpty(builder.cacheStr)) {
            CacheSDK.put(IType.ICache.DIALOG_SHOW + builder.cacheStr, true);
        }
    }

    public void setOnDismissListener(OnDismissListener listener) {
        dismissListener = listener;
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
        if (dismissListener != null) {
            dismissListener.onDismiss();
        }
    }
}
